{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T11:29:36.285524Z",
     "start_time": "2024-12-30T11:29:36.268506Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import tensorflow as tf\n",
    "import warnings\n"
   ],
   "id": "d05a6917a51d1337",
   "outputs": [],
   "execution_count": 34
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-12-30T11:29:36.301508Z",
     "start_time": "2024-12-30T11:29:36.287507Z"
    }
   },
   "source": [
    "# tf.Variable生成的变量，每次迭代都会变化，\n",
    "# 这个变量也就是我们要去计算的结果，所以说你要计算什么，你是不是就把什么定义为Variable\n",
    " \n",
    "# Variable 是 TensorFlow 中用于保存状态的对象，可以在计算图中修改。\n",
    "# TensorFlow 变量 x，其初始值为 3\n",
    "x = tf.Variable(3, name='x') \n",
    "print(x)\n",
    "\n",
    "# TensorFlow 变量 y，其初始值为 4\n",
    "y = tf.Variable(4, name='y')\n",
    "print(y)\n",
    "\n",
    "\n",
    "f = x * x * y + y + 2\n",
    "print(f)\n"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<tf.Variable 'x_16:0' shape=() dtype=int32_ref>\n",
      "<tf.Variable 'y_16:0' shape=() dtype=int32_ref>\n",
      "Tensor(\"add_33:0\", shape=(), dtype=int32)\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T11:29:36.317531Z",
     "start_time": "2024-12-30T11:29:36.302509Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "461ae994cda93902",
   "outputs": [],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T11:29:36.348511Z",
     "start_time": "2024-12-30T11:29:36.318508Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建一个计算图的一个上下文环境\n",
    "# 配置里面是把具体运行过程在哪里执行给打印出来\n",
    "sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))\n",
    "# 碰到session.run()就会立刻去调用计算\n",
    "sess.run(x.initializer)\n",
    "sess.run(y.initializer)\n",
    "result = sess.run(f)\n",
    "print(result)\n",
    "sess.close()\n"
   ],
   "id": "44856e393d125428",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42\n"
     ]
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T11:29:36.364524Z",
     "start_time": "2024-12-30T11:29:36.349511Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 如果你使用的是 TensorFlow 2.x，那么可以将代码修改为不使用会话的方式。下面是一个示例，展示如何使用 TensorFlow 2.x 创建和计算图：\n",
    "\n",
    "# 直接打印结果，因为在 Eager Execution 模式下\n",
    "# print(f.numpy())  # 使用 .numpy() 方法获取数值\n"
   ],
   "id": "93c0120496a013cf",
   "outputs": [],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-12-30T11:29:36.379514Z",
     "start_time": "2024-12-30T11:29:36.365512Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "251460cd34459be",
   "outputs": [],
   "execution_count": 37
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
